var express = require('express');
var session = require('express-session');
var agent_service = require('../service/agent_server');
var path = require('path');
var wx_auth =require("../utils/wx_auth");
var userMgr = require("../service/userMgr");
var http = require("../utils/http");
var qr = require('qr-image')


// 可以直接使用req.cookies.cname来访问cookie
var cookie = require('cookie-parser');
var log4js = require('log4js');

var app = express();
var hallAddr = "";
var imgList = {};

function send(res,ret){
	var str = JSON.stringify(ret);
	res.send(str);
	return;
}

var config = null;

exports.start = function(cfg){
	config = cfg;
	app.listen(config.port);
	console.log("agent server is listening on " + config.port);
};

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


var valiteUrls =['/getUserGems','/updateClub','/getAgentBalance','/delteFromClub','/getClubUserList','/createClub','/auditApply','/getClubApplyList','/getClubList','/getUserInfo','/getStaticData','/getAgenPagetList','/getPlayerCountById','/getAgentCountById','/getPlayerPageList','/charge','/setAgent',];


//简单实现拦截器
app.use(function(req,res,next){
	var url = req.url;
	var index =url.indexOf("?");
	url = url.substring(0,index);
	
	if(valiteUrls.indexOf(url)!=-1){
		var id = req.query.id;
		console.log(req.url + "  进入id :" + id);
		if(userMgr.isLogin(id)){
			userMgr.freshActiveTime(id);
			next();
		}else{
			//未登录 返回
			send(res,{errcode:-999,errmsg:"用户未登录"});
			console.log(req.url + "  进入id :" + id  +"  用户未登录");
			//res.redirect("/wxlogin");
		}
	}else{
		next();//如果请求的地址是登录则通过，进行下一个请求
	}
});				
//静态文件访问
app.use(express.static(path.join(__dirname, '../public')))

app.get("/test",function(req,res){
	var httpurl = "http://qr.topscan.com/api.php?text=http://www.baidu.com";
//http.downLoad(httpurl,"test.png");
	
});


//用户登录
app.get("/login",function(req,res){

	var id = req.query.userId;
	var password = req.query.pwd;  //密码使用邀请码
	if(id==null ||password==null){
		send(res,{errcode:1,errmsg:"param  is null"});
	}else{
		agent_service.login_userid(id,password,function(ret){
			console.log("用户登录login ",ret);
			var code = ret.code;
			var data = ret.data;
			if(code==0){
				if(data==null){
					send(res,{errcode:3,errmsg:"账号不存在"});
				}else{
				//	console.info(data);
					if(data.role_id==5){
						send(res,{errcode:4,errmsg:"不是代理商，没有权限登录"});
					}else{
						//登录成功 保存登录信息
						var user = {
							 id:data.id,  //用户id
							 userId:data.user_id,
							 role_id:data.role_id,
							 isLogin:1,  //是否登录 1登录 0 未登录
							 activeTime:new Date().getTime()  //activeTime 默认10分钟超时
							};
						console.log("代理商登录成功  ：userId " ,data );
						userMgr.saveUser(user);
						send(res,{errcode:0,data:data});
					}
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param  is null"});
			}else{
				console.error("login error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}

});


// //登录  url wxlogin?to=game_role_agentId
app.get('/wxlogin',function(req,res){
	//代理代理的表示注册在代理名下 没有可以注册 没带代理的 没有直接返回
	var agent = req.query.agent;
	
	if(agent == null){
		agent = 0;
	}
	var url = config.cburl+ "wxcallback/"+agent;
	//发起微信用户授信  重定向到微信授权页面
	console.log("回调",url);
	 var wx_url = wx_auth.getCode(url);
	  console.log('微信url ',wx_url);
	 res.redirect(wx_url);
	 return;

});


app.get('/wxcallback/:agent',function(req,res){
	var code = req.query.code;
	var state = req.query.state;
	var agent = req.params.agent;
	console.log("微信登录回调 code = " + code  + " state = " + state + " agent = " + agent  );
    if(code!=null){
	wx_auth.wxAuth(code,function(result,data){
		if(result){
			var openid = data.openid;
			var nickname = data.nickname;
			var sex = data.sex;
			var province = data.province;
			var city = data.city;
			var country = data.country;
			var headimgurl = data.headimgurl;
			var unionid = data.unionid;
			
			console.log("微信登录成功..",data);
			agent_service.agent_login(openid,nickname,sex,headimgurl,unionid,agent,function(ret){

			var code = ret.code;
			var data = ret.data;
			console.log("agent_login : " ,ret);
			if(code==0){
				var t = data[0];
				if(t.code==0){
				//登录成功 保存登录信息
				var user = {
					id:t.agentId,  //用户id									
					isLogin:1,  //是否登录 1登录 0 未登录
				    activeTime:new Date().getTime()  //activeTime 默认10分钟超时
					};
					console.log("代理商登录成功 " ,data );
					
					userMgr.saveUser(user);

					//成功跳转
					console.log("登录跳转到protal ",config.portal_url +t.code +"/"+t.agentId);
					res.redirect(config.portal_url +t.code +"/"+t.agentId);
				}else{
					//失败跳转
					res.redirect(config.portal_url +"log/" +t.code +"/"+t.agentId);
				}
				
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("agent_login error  " + data);
				//send(res,{errcode:2,errmsg:'db error'});
				res.redirect(config.portal_url +"log/2/0");
			}
			});
		}else{
			console.log("微信登录失败.." + data);
			send(res,{errcode:1,errmsg:"微信校验失败"});	
		}
	});
	}else{
		console.log('用户拒绝授权');
		send(res,{errcode:1,errmsg:"用户拒绝授权"});	
	}
});


//获取用户信息展示信息 包括角色 和权限  通过登录获取agentId 代理商id
app.get('/getUserInfo',function(req,res){

	var id = req.query.id;
	if(id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		agent_service.getUserInfo(id,function(ret){

			console.log("getUserInfo: ",ret);
			var code = ret.code;
			var data = ret.data;
			if(code==0){
				if(data==null){
				send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getUserInfo error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}

		});
	}
});

//通过userid 获取代理和玩家总数
app.get("/getStaticData",function(req,res){
	var id =  req.query.id;
	var role_id =req.query.role_id;
	if(id==null){
			send(res,{errcode:1,errmsg:"param id is null"});
		}else{
			agent_service.getStaticData(id,role_id,function(ret){
			console.log("getStaticData :" ,ret);
			var code = ret.code;
			var data = ret.data;
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getStaticData error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
		}

});


//分页获取代理商list集合
app.get("/getAgenPagetList",function(req,res){

	var pageStart = req.query.start;//开始页
	var pageCount = req.query.limit;
	var id = req.query.id;
	var role_id = req.query.role_id;
	if(pageStart==null){
		pageStart=0;
	}
	if(pageCount==null){
		pageCount=30;
	}
	if(id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		agent_service.getAgentList(id,pageStart,pageCount,role_id,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getAgenPagetList : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					agent_service.getAgentCount(id,role_id,function(rets){
						if(rets.code==0 &&rets.data!=null){
							send(res,{errcode:0,pageCount:rets.data.total,data:data});
						}else{
							send(res,{errcode:4,errmsg:'获取代理商总数失败'});
						}
					});
					
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getAgentListById error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
			
		});
	}
});

//获取玩家list集合
app.get("/getPlayerPageList",function(req,res){

	var pageStart = req.query.start;//开始页
	var pageCount = req.query.limit;
	var id =req.query.id;
	var role_id = req.query.role_id;
	if(pageStart==null){
		pageStart=0;
	}
	if(pageCount==null){
		pageCount=30;
	}
	if(id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		agent_service.getPlayerList(id,pageStart,pageCount,role_id,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getPlayerPageList : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
						agent_service.getPlayerCount(id,role_id,function(rets){
						if(rets.code==0 &&rets.data!=null){
							send(res,{errcode:0,pageCount:rets.data.total,data:data});
						}else{
							send(res,{errcode:4,errmsg:'获取玩家总数失败'});
						}
					});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getPlayerListById error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


//代理房卡充值
app.get("/charge",function(req,res){

	var receiveId =  req.query.receiveId; //被充值id
	var sendId = req.query.sendId; //充值id
	var num = req.query.num;//充值金额
	if(receiveId==null || sendId==null || num==null){
		send(res,{errcode:1,errmsg:"params is null"});
	}else{
		var params = {
			receiveId:receiveId,
			sendId:sendId,
			num:num
		};
		agent_service.charge(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("charge : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					var t = data[0];

					//send(res,{errcode:0,data:data});
					send(res,{errcode:t.code,errmsg:t.msg});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getPlayerListById error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


//设置代理
app.get('/setAgent',function(req,res){

	var pagentId=req.query.pagentId;//自己id
	var agentId = req.query.agentId;//需要设置的代理
	//var agentType = req.query.agentType;//
	
    if(pagentId==null || agentId==null){
		send(res,{errcode:1,errmsg:"params is null"});
	}else{
		var params = {
			pagentId:pagentId,
			agentId:agentId
		};
		agent_service.setAgent(params,function(ret){
			var code = ret.code;
			var data = ret.data;
		    console.log("setAgent : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					var t = data[0];
					//send(res,{errcode:0,data:data});
					send(res,{errcode:t.code,errmsg:t.msg});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getPlayerListById error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}

});


//获取代理商详情查询
app.get('/getAgentBalance',function(req,res){

	var agentId=req.query.agentId;
	var start_time = req.query.startTime;
	var end_time = req.query.endTime;
   // var role_id = userMgr.get(agentId).role_id;

    if(agentId==null ){
		send(res,{errcode:1,errmsg:"params is null"});
		return;
	}

	
	var param = {
		agentId : agentId,
		start_time :start_time,
		end_time:end_time
	};
	agent_service.getAgentBalance(param,function(ret){
			var code = ret.code;
			var data = ret.data;
		    console.log("setAgent : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					
					send(res,{errcode:0,data:data});
				
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getPlayerListById error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
});



//用户提现
app.get('/payAgent',function(req,res){

	var agentId=req.query.agentId; //操作人
	var childUserId = req.query.childUserId;//提现人
	var money = req.query.money;  //提现金额
	
	var role_id = req.query.role_id;
	console.log("代理提现",agentId+"--"+childUserId +'--'+money);

	
	
    if(agentId==null || money == null || childUserId==null||role_id==null){
		send(res,{errcode:1,errmsg:"params11 is null"});
		return;
	}
	
    if(role_id !=1){
			send(res,{errcode:2,errmsg:"只有管理员才能进行提现操作"});
			return;
	}
	
	var param = {
		agentId : agentId,
		childUserId :childUserId,
		money:money
	};
	agent_service.payAgent(param,function(ret){
			var code = ret.code;
			var data = ret.data;
		    console.log("payAgent : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
				
					send(res,{errcode:0,data:data});
				
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else if (code ==3){
				send(res,{errcode:2,errmsg:'余额不足'});
			}else{
				console.error("getPlayerListById error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
});

//获取玩家list集合
app.get("/getClubList",function(req,res){

	var id =req.query.id;
	var role_id = req.query.role_id;
	
	if(id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		agent_service.getClubList(id,role_id,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getClubList : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("获取俱乐部list error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


//获取俱乐部申请记录list（待审核）
app.get("/getClubApplyList",function(req,res){

	var id =req.query.id;
	var club_id = req.query.club_id;
	
	if(id==null || club_id ==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		agent_service.getClubApplyList(club_id,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getClubApplyList : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("获取俱乐部申请记录list error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});



//审核
app.get("/auditApply",function(req,res){

	var id =req.query.id;
	var apply_id = req.query.apply_id;
	var status = req.query.status;
	
	if(apply_id==null || status ==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		agent_service.auditApply(apply_id,status,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("auditApply : " ,ret);
			if(code==0){
			
				send(res,{errcode:0,data:data[0]});
				
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("auditApply error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


//审核
app.get("/createClub",function(req,res){

	var id =req.query.id;
	var user_id = req.query.user_id;
	var club_name = req.query.club_name;

	var gems = req.query.gems;
	
	if(id==null || user_id ==null ||club_name==null||gems==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{

		var params ={
			id:id,
			user_id:user_id,
			club_name:club_name,
			
			gems:gems
		};
		agent_service.createClub(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("createClub : " ,ret);
			if(code==0){
			
				send(res,{errcode:0,data:data[0]});
				
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("createClub error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


//获取俱乐部申请记录list（待审核）
app.get("/getClubUserList",function(req,res){

	var id =req.query.id;
	var club_id = req.query.club_id;
	var start = req.query.start;
	var limit = req.query.limit;
	
	console.log('getClubUserList');
	if(id==null || club_id ==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{

		if(start==null){
			start = 0;
		}
		if(limit ==null){
			limit = 10;
		}

		var params = {

			id:id,
			club_id:club_id,
			start:start,
			limit:limit
		};
		agent_service.getClubUserList(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getClubUserList : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("获取俱乐部申请记录list error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});



//获取俱乐部申请记录list（待审核）
app.get("/delteFromClub",function(req,res){

	var id =req.query.id;
	var club_id = req.query.club_id;
	var user_id = req.query.user_id;

	

	if(id==null || club_id ==null ||user_id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{


		var params = {

			id:id,
			club_id:club_id,
			user_id:user_id
			
		};
		agent_service.delteFromClub(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("delteFromClub : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else if(code == 3){
				send(res,{errcode:3,errmsg:"删除失败:创建者才能删除俱乐部成员"});
			}else{
				console.error("获取俱乐部申请记录list error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});

//修改俱乐部
app.get("/updateClub",function(req,res){

	var id =req.query.id;
	var club_id = req.query.club_id;
	var user_id = req.query.user_id;
	var charge_type = req.query.charge_type;
	var gems = req.query.gems;

	

	if(id==null || club_id ==null ||user_id==null||charge_type==null||gems==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{


		var params = {

			id:id,
			club_id:club_id,
			charge_type:charge_type,
			gems:gems,
			user_id:user_id
			
		};
		agent_service.updateClub(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("updateClub : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data[0]});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else if(code == 3){
				send(res,{errcode:3,errmsg:"updateClub error"});
			}else{
				console.error("updateClub error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});



//获取俱乐部申请记录list（待审核）
app.get("/getUserGems",function(req,res){

	var id =req.query.id;
	var user_id = req.query.user_id;
	if(id==null || user_id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		var params = {
			id:id,
			user_id:user_id
		};
		agent_service.getUserGems(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getUserGems : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else if(code == 3){
				send(res,{errcode:3,errmsg:"获取用户房卡"});
			}else{
				console.error("获取俱乐部申请记录list error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});
//获取代理商联系信息
app.get("/getAgentPayInfo",function(req,res){

	var id =req.query.id;
	
	if(id==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{
		var params = {
			id:id
		};
		agent_service.getAgentPayInfo(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getAgentPayInfo : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:3,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else{
				console.error("getAgentPayInfo error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});
//修改代理联系信息
app.get("/editAgentPay",function(req,res){

	var id =req.query.id;

	var name = req.query.name;
	var mobile = req.query.mobile;
	var alipayAccount = req.query.alipayAccount;
	if(id==null  || name==null || mobile==null ||alipayAccount==null ){
		send(res,{errcode:1,errmsg:"param is null"});
	}else{
		var params = {
			id:id,
			name:name,
			mobile:mobile,
			alipayAccount:alipayAccount
		};
		agent_service.editAgentPay(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getUserGems : " ,ret);
			if(code==0){
				
				send(res,{errcode:0,msg:'success'});
			}else{
				console.error("修改代理联系信息 error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


//分页获取俱乐部房间新
app.get("/getClubRoom",function(req,res){

	var id =req.query.id;
	var club_id = req.query.club_id;
	//var start = req.query.start;
	//var limit = req.query.limit;
	var startTime = req.query.startTime;
	var endTime = req.query.endTime;
	
	
	if(id==null || club_id ==null){
		send(res,{errcode:1,errmsg:"param id is null"});
	}else{

		// if(start==null){
		// 	start = 0;
		// }
		// if(limit ==null){
		// 	limit = 10;
		// }

		var params = {

			id:id,
			club_id:club_id,
			startTime:startTime,
			endTime:endTime
			// start:start,
			// limit:limit
		};
		agent_service.getClubRoom(params,function(ret){
			var code = ret.code;
			var data = ret.data;
			console.log("getClubRoom : " ,ret);
			if(code==0){
				if(data==null){
					send(res,{errcode:2,errmsg:"no data"});
				}else{
					send(res,{errcode:0,data:data});
				}
			}else if(code==1){
				send(res,{errcode:1,errmsg:"param id is null"});
			}else{
				console.error("getClubRoom error  " + data);
				send(res,{errcode:2,errmsg:'db error'});
			}
		});
	}
});


app.get("/shareImg",function(req,res){
	var agentId = req.query.agent_id;
	if(agentId==null){
		//玩家分享
		res.redirect(config.cburl+"/static/img/share-player.jpg");
		return;
	}else{
		//代理分享
		var fileName = "agent-"+agentId+".jpg";
		var params = {

				"serverUrl":config.cburl,
				"agentId":agentId

		};
		console.log("获取二维码地址");
		var count=1;
  		http.downLoadShare(params,fileName,function(ret){

				console.log("获取二维码",ret);
				if(ret.code==-1){
					//二维码下载失败，直接生产
					var text = config.cburl+"wxlogin?agent="+agentId;
					try {
						var img = qr.image(text,{size :10});
						res.writeHead(200, {'Content-Type': 'image/png'});
						img.pipe(res);
						return
					} catch (e) {
						
						send(res,{errcode:0,errmsg:config.cburl+"wxlogin?agent="+agentId });
					}
					
					
					return;
				}else{
					res.redirect(config.cburl+"/static/img/" +fileName);
					return;
				}

		  });
	}

	
	
});





